<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item><a href="javascript:void(0)" @click="findChildren(0,1)">期望职位</a></el-breadcrumb-item>
      <el-breadcrumb-item v-for="p in parent" :key="p.id">
        <a href="javascript:void(0)" @click="findChildren(p,1)">{{ p.name }}</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <hr>
    <el-form>
      <el-row style="margin-bottom: 10px">
        <el-button type="success" @click="openDialog(null)" icon="el-icon-plus" size="small">新增</el-button>
        <el-button type="info" @click="deleteIds" size="small" icon="el-icon-delete">批量删除</el-button>
      </el-row>
    </el-form>
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="selectIds">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="职位ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="职位名称">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="obj">
          <el-button v-if="parent.length<2" @click="findChildren(obj.row,2)" size="small">查询下级</el-button>
          <el-button @click="openDialog(obj.row.id)" size="small">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%">

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="父级分类">
          <el-breadcrumb style="margin-top: 11px;" separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>期望职位</el-breadcrumb-item>
            <el-breadcrumb-item v-for="(p,index) in parent" :key="p.id">
              <span v-if="index!==(parent.length-1)">{{ p.name }}</span>
              <b v-if="index===(parent.length-1)">{{ p.name }}</b>
            </el-breadcrumb-item>
          </el-breadcrumb>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "position-manage",
  data () {
    return {
      tableData:[],
      pid:0,
      parent:[],
      dialogVisible:false,
      form:{},
      ids:[]
    }
  },
  methods:{
    query:function () {
      this.$http({
        url: this.$http.adornUrl('/hope/position/grouping/'+this.pid),
        method: 'get'
      }).then(({data}) => {
        this.tableData = data;
      })
    },
    findChildren:function (row,type) {
      if(this.parent.indexOf(row)==1){
        //用户点击的是第二个父级
        return;
      }
      if(row==0){
        //点击首页
        this.pid = 0;
        this.parent = [];
      }else if(type==2){
        //点击"查看下级"
        this.parent.push(row);
        this.pid = row.id;
      }else{
        //点击"面包屑"，并且点的不是首页
        this.parent = [];
        this.parent.push(row);
        this.pid = row.id;
      }
      this.query();
    },
    openDialog:function (id) {
      if(id==null){
        this.form = {
          parentId: 0,
          typeId: null
        }
      }else{
        this.$http({
          url: this.$http.adornUrl('/hope/position/grouping?id='+id),
          method: 'get'
        }).then(({data}) => {
          this.form = data;
        })
      }
      this.dialogVisible = true;
    },
    save:function () {
      if(this.parent.length>0){
        var obj = this.parent[this.parent.length-1];
        this.form.pid = obj.id;
      }
      this.$http({
        url: this.$http.adornUrl(`/hope/position/${!this.form.id ? 'save' : 'update'}`),
        method: 'post',
        data: this.$http.adornData({
          'id': this.form.id || undefined,
          'name': this.form.name,
          'pid': this.form.pid
        })
      }).then(({data}) => {
        if (data.msg==='success'){
          this.$message({
            message: '职位数据保存成功！',
            type: 'success',
            duration: '1500',
            onClose(){
              location.reload();
            }
          });
        }else{
          this.$message({
            message: '职位数据保存错误！',
            type: 'error',
            duration: '1500',
            onClose(){
              location.reload();
            }
          });
        }
      })
    },
    selectIds:function (obj) {
      this.ids = [];
      for (let i=0;i<obj.length;i++){
        this.ids.push(obj[i].id);
      }
    },
    deleteIds:function () {
      if(this.ids.length==0){
        this.$message({
          message: '请选择要删除的数据！',
          type: 'error'
        })
      }else{
        this.$http({
          url: this.$http.adornUrl('/hope/position/delete'),
          method: 'delete',
          data: this.$http.adornData(this.ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                location.reload();
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  },
  created() {
    this.query();
  }
}
</script>

<style scoped>

</style>
